1535D - Playoff Tournament - CodeForces Solution


data structures dfs and similar dp implementation trees *1800

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
const int N = (1<<18) + 5;
int k, n, q, dp[N], nIdx[N], idx[N];
string s;
void go(int i, int lvl, int x){
  if(lvl == x){
    dp[i] = ((s[nIdx[i]] == '?') ? 2 : 1);
    return;
  }
  go(2*i, lvl+1, x);
  go(2*i + 1, lvl+1, x);
  if(s[nIdx[i]] == '?'){
    dp[i] = dp[2*i] + dp[2*i + 1]; 
  } else if(s[nIdx[i]] == '1'){
    dp[i] = dp[2*i + 1]; 
  } else{
    dp[i] = dp[2*i];
  }
}
void build(string &str){
  int len = size(str);
  int lvl = __builtin_popcount(len) - 1, cur = 0;
  for(int k = lvl; k >= 0; k--){
    for(int j = 0; j < (1<<k); j++){
      idx[cur] = (1<<k)+j;
      nIdx[(1<<k)+j] = cur++;
    }
  }
  go(1, 0, lvl);
}
void update(int pos, int mx){
  if(2*pos > mx){
    if(s[nIdx[pos]] == '?')dp[pos] = 2;
    else dp[pos] = 1;
  }else{ 
    if(s[nIdx[pos]] == '?')
      dp[pos] = dp[pos<<1] + dp[pos<<1|1];
    else if(s[nIdx[pos]] == '1')dp[pos] = dp[pos<<1|1];
    else dp[pos] = dp[pos<<1];
  }
  if(pos/2 > 0)update(pos/2, mx);
}

int main()
{
  cin.tie(0) -> sync_with_stdio(false);
  cin>>n>>s>>q;
  build(s);
  int mx = s.size();
  while(q--){
    int pos; char x;
    cin>>pos>>x;
    char xx = s[--pos];
    s[pos] = x;  update(idx[pos], mx); 
    cout<<dp[1]<<'\n';
  }
}


Comments

Submit
0 Comments
More Questions

1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut
1101A - Minimum Integer
985D - Sand Fortress
1279A - New Year Garland
1279B - Verse For Santa
202A - LLPS
978A - Remove Duplicates
1304A - Two Rabbits
225A - Dice Tower
1660D - Maximum Product Strikes Back
1513A - Array and Peaks
1251B - Binary Palindromes
768B - Code For 1
363B - Fence
991B - Getting an A
246A - Buggy Sorting
884A - Book Reading
1180A - Alex and a Rhombus
445A - DZY Loves Chessboard
1372A - Omkar and Completion